import React, { useEffect, useState } from 'react';
import orgCtrl from '@/ts/controller';
import { ICompany, IWorkTask } from '@/ts/core';
import { useFlagCmdEmitter } from '@/hooks/useCtrlUpdate';
import FullScreenModal from '@/components/Common/fullScreen';
import cls from '../../index.module.less';
import HomeWork from '../../work';
import HomeChat from '../../chat';
import { IReception } from '@/ts/core/work/assign/reception';
import { IAssignTask } from '@/ts/core/work/assign';
import { useSessionContext } from '../../../content';
import { useHistory } from 'react-router-dom';

interface IProps {
  company: ICompany;
}
interface IWorkList {
  cmd: number;
  title: string;
  count: number;
  content?: IAssignTask[];
}
// 事项详情
const MatterInfo: React.FC<IProps> = (props) => {
  const [openType, setOpenType] = useState<IWorkList | undefined>(undefined);
  const { updateContent } = useSessionContext();
  const history = useHistory();
  const [workList, setWorkList] = useState<IWorkList[]>([
    {
      cmd: 4,
      title: '消息',
      count: 0,
    },
    {
      cmd: 2,
      title: '动态',
      count: 0,
    },
    {
      cmd: 1,
      title: '待办',
      count: 0,
    },
    {
      cmd: 3,
      title: '任务',
      count: 0,
    },
    {
      cmd: 5,
      title: '订单',
      count: 0,
    },
  ]);
  useFlagCmdEmitter('session', () => {
    setMessageCount();
  });

  const findTodo = (todos: IWorkTask[], groupName: string) => {
    return todos.filter((item: IWorkTask) => {
      return item.groupTags.includes(groupName);
    });
  };

  const setMessageCount = () => {
    const _workList = [...workList];
    _workList[0].count = props.company.session.messages.length;
    _workList[1].count = props.company.session.activity.activityList.length;
    setWorkList(_workList);
  };

  const setWorkCountFn = () => {
    const workId = orgCtrl.work.notity.subscribe(async () => {
      const _workList = [...workList];
      _workList[2].count = findTodo(orgCtrl.work.todos, props.company.name).length;
      setWorkList(_workList);
    });
    orgCtrl.work.notity.unsubscribe(workId);
  };

  useEffect(() => {
    setMessageCount();
    setWorkCountFn();
    loadReceptions();
    loadOrder();
  }, [props.company]);

  const loadReceptions = async () => {
    const _workList = [...workList];
    const tasks = await props.company.loadAssignTasks();
    _workList[3].content = tasks;
    _workList[3].count = tasks.length;
    setWorkList(_workList);
  };

  const loadOrder = async () => {
    const _workList = [...workList];
    const orders = await props.company.loadAllOrder();
    _workList[3].content = orders;
    _workList[3].count = orders.length;
    setWorkList(_workList);
  };

  const renderCmdBtn = (item: IWorkList) => {
    const { cmd, title, count } = item;
    return (
      <div
        className={cls['matter-item']}
        onClick={() => {
          setOpenType(item);
          jump(item);
        }}>
        <div>
          <img src={'/img/home/cardItem/card' + cmd + '.png'} alt="" />
          <span>{title}</span>
        </div>
        <div className={cls['matter-number']}>{count}</div>
      </div>
    );
  };

  const jump = (item: IWorkList) => {
    if (item?.title == '消息') {
      updateContent('chat');
    } else if (item?.title == '动态') {
      updateContent('activity');
    }
  };
  return (
    <>
      <div className="cardGroup">
        <div className="cardItem">
          <div className="cardItem-header">
            <span className="title">概览</span>
          </div>
          <div style={{ width: '100%', minHeight: 60 }} className={cls['matter-content']}>
            {workList.map((item) => {
              return renderCmdBtn(item);
            })}
          </div>
          {/* <FullScreenModal
            open={!!openType}
            title={openType ? props.company.name + '的' + openType?.title : '事项'}
            width={'80vw'}
            bodyHeight={'70vh'}
            onCancel={() => setOpenType(undefined)}>
          </FullScreenModal> */}
        </div>
      </div>
    </>
  );
};
export default MatterInfo;
